<template>
  <div class="info">
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input style="width: 50%" v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="类型" prop="media_type">
        <el-radio-group v-model="ruleForm.media_type">
          <el-radio :label="1">公众号文章</el-radio>
          <el-radio :label="2">自定义内容</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.media_type === 1"
        label="跳转链接"
        prop="link"
      >
        <el-input style="width: 50%" v-model="ruleForm.link"></el-input>
      </el-form-item>

      <el-form-item label="图片">
        <el-upload
          :class="{ hide: hideUpload }"
          action="test"
          list-type="picture-card"
          :on-remove="handleRemoveImage"
          :limit="1"
          :file-list="ruleForm.images"
          :http-request="uploadImage"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.media_type === 2"
        label="内容"
        prop="content"
      >
        <tinymce v-model="ruleForm.content" :height="300" />
      </el-form-item>
    </el-form>
    <div class="action">
      <el-button type="primary" @click="submitForm">立即保存</el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import { getInfo, update, add } from "@/api/ads";
import { uploadFile } from "@/api/common";
export default {
  name: "carouselInfo",
  components: { Tinymce },

  data() {
    return {
      id: this.$route.query.id,
      hideUpload: false,
      ruleForm: {
        title: "",
        media_type: 1,
        content: "",
        link: "",
        images: [],
      },
    };
  },
  mounted() {
    if (this.id) {
      this.getInfo();
    }
  },
  methods: {
    async getInfo() {
      const res = await getInfo({ id: this.id });
      if (res.data) {
        const obj = res.data;
        if (obj.image) {
          obj.images = obj.image
            ? [
                {
                  url: obj.image,
                },
              ]
            : [];
        }

        this.hideUpload = obj.images.length === 1;
        this.ruleForm = obj;
      }
    },
    async uploadImage({ file }) {
      const formdata = new FormData();
      formdata.append("file", file);
      formdata.append("dir", "carousel");
      formdata.append("filetype", "image");

      const res = await uploadFile(formdata);
      this.ruleForm.images.push({
        id: 0,
        attachment: res.data.filename,
        status: "success",
        name: res.data.filename,
        url: res.data.filepath,
      });
      this.hideUpload = this.ruleForm.images.length >= 1;
    },
    handleRemoveImage(file, fileList) {
      this.ruleForm.images = fileList;
      this.hideUpload = fileList >= 1;
    },

    async submitForm() {
      const postData = Object.assign({}, this.ruleForm);
      if (postData.images && postData.images.length) {
        postData.image = postData.images[0].url;
      }
      const func = this.id ? update : add;
      const res = await func(postData);
      if (res) {
        this.$message({
          message: res.msg,
          type: "success",
        });
        this.$router.go(-1);
      }
    },
  },
};
</script>
<style lang="scss">
.action {
  padding-left: 100px;
}
.hide {
  .el-upload--picture-card {
    display: none;
  }
}
</style>